~ chicken-core (master) /manual/Module (chicken random)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken random)
5
6This module provides access to entropy (as provided by the operating
7system) and a pseudo random number generator.
8
9On UNIX systems entropy is by default obtained from {{/dev/urandom}}.
10On Linux, the {{getrandom(2)}} system call is used instead, if available.
11On OpenBSD it is {{arc4random_buf(3)}}, on Windows {{RtlGenRandom}}.
12
13The pseudo random number generator is an implementation of the
14[[https://en.wikipedia.org/wiki/Well_equidistributed_long-period_linear|WELL]] algorithm.
15
16==== set-pseudo-random-seed!
17
18<procedure>(set-pseudo-random-seed! SEED [SIZE])</procedure>
19
20Set seed for the PRNG from the at most {{SIZE}} bytes of {{SEED}}.
21which should be a bytevector containing random data.
22If {{SIZE}} is not given, it defaults to the size of {{SEED}}. If {{SIZE}}
23(or the size of {{SEED}}) is below the required size of the random
24state used by the PRNG, then it will necessarily be initialized in a less
25random manner as possible.
26
27
28==== pseudo-random-integer
29
30<procedure>(pseudo-random-integer RANGE)</procedure>
31
32Returns a uniformly distributed pseudo random integer between 0 and
33{{RANGE-1}}, which may be a big integer.
34
35
36==== pseudo-random-real
37
38<procedure>(pseudo-random-real)</procedure>
39
40Returns a uniformly distributed pseudo-random inexact number between
410 and 1.
42
43
44==== random-bytes
45
46<procedure>(random-bytes [BUF [SIZE]])</procedure>
47
48Returns random bytes from the available entropy source. If {{BUF}} is
49given, it should be a string or byte vector which will be filled with up to
50{{SIZE}} random bytes. {{SIZE}} defaults to the size of the {{BUF}}
51argument. If no arguments are given, {{random-bytes}} returns a
52freshly allocated byte vector of sufficient size to be used as a seed
53for {{set-pseudo-random-seed!}}.
54
55
56---
57Previous: [[Module (chicken process-context)]]
58
59Next: [[Module (chicken read-syntax)]]